﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ColorPane.Control
{
    /// <summary>
    /// HSVColorPane.xaml 的交互逻辑
    /// </summary>
    public partial class HSVColorPane : UserControl
    {

        #region dependencyProperty

        public readonly static DependencyProperty ThicknessProperty = 
            DependencyProperty.Register("Thickness",typeof(double),typeof(HSVColorPane)
            ,new PropertyMetadata(1d));

        public double Thickness {
            get => (double) this.GetValue( ThicknessProperty );
            set {
                this.SetValue( BorderThicknessProperty, value );
                UpdateThickness( value );
            }
        }

        bool Lock;
        Effects.ColorWheelEffect Shader;

        public readonly static DependencyProperty HueProperty=
            DependencyProperty.Register("Hue",typeof(double),typeof(HSVColorPane),new PropertyMetadata(0d));
        public double Hue {
            get => (double) this.GetValue( HueProperty );
            set {
                this.SetValue( HueProperty, value );
                UpdateRGB( PixelHelper.HSV2RGB( Hue, Saturation, Value ) );
            }
        }

        public readonly static DependencyProperty SaturationProperty=
            DependencyProperty.Register("Saturation",typeof(double),typeof(HSVColorPane),new PropertyMetadata(0d));
        public double Saturation {
            get => (double) this.GetValue( SaturationProperty );
            set {
                this.SetValue( SaturationProperty, value );
                UpdateRGB( PixelHelper.HSV2RGB( Hue, Saturation, Value ) );
            }
        }

        public readonly static DependencyProperty ValueProperty=
            DependencyProperty.Register("Value",typeof(double),typeof(HSVColorPane),new PropertyMetadata(0d));
        public double Value {
            get => (double) this.GetValue( ValueProperty );
            set {
                this.SetValue( ValueProperty, value );
                UpdateRGB( PixelHelper.HSV2RGB( Hue, Saturation, Value ) );
            }
        }


        public readonly static DependencyProperty RedProperty=
            DependencyProperty.Register("Red",typeof(double),typeof(HSVColorPane),new PropertyMetadata(0d));
        public double Red {
            get => (double) this.GetValue( RedProperty );
            set {
                this.SetValue( RedProperty, value );
                UpdateHSV( PixelHelper.RGB2HSV( Red, Green, Blue ) );
            }
        }

        public readonly static DependencyProperty AlphaProperty=
            DependencyProperty.Register("Alpha",typeof(double),typeof(HSVColorPane),new PropertyMetadata(0d));
        public double Alpha {
            get => (double) this.GetValue( AlphaProperty );
            set {
                this.SetValue( AlphaProperty, value );
            }
        }

        public readonly static DependencyProperty BlueProperty=
            DependencyProperty.Register("Blue",typeof(double),typeof(HSVColorPane),new PropertyMetadata(0d));
        public double Green {
            get => (double) this.GetValue( GreenProperty );
            set {
                this.SetValue( GreenProperty, value );
                UpdateHSV( PixelHelper.RGB2HSV( Red, Green, Blue ) );
            }
        }

        public readonly static DependencyProperty GreenProperty=
            DependencyProperty.Register("Green",typeof(double),typeof(HSVColorPane),new PropertyMetadata(0d));
        public double Blue {
            get => (double) this.GetValue( BlueProperty );
            set {
                this.SetValue( BlueProperty, value );
                UpdateHSV( PixelHelper.RGB2HSV( Red, Green, Blue ) );
            }
        }

        public readonly static DependencyProperty SoildBrushProperty=
            DependencyProperty.Register("SoildBrush",typeof(SolidColorBrush),typeof(HSVColorPane),new PropertyMetadata(new  SolidColorBrush(Colors.Red)) );
        public SolidColorBrush SoildBrush {
            get => ( new SolidColorBrush( (Color) this.GetValue( SoildBrushProperty ) ) );
            set {
                this.SetValue( SoildBrushProperty, value );
            }
        }

        #endregion

        public HSVColorPane()
        {
            InitializeComponent();
            Shader = Wheel.Effect as Effects.ColorWheelEffect;
            this.SizeChanged += HSVColorPane_SizeChanged;
        }

        private void HSVColorPane_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            SVPane.Width = ActualWidth - Thickness * ActualWidth / 2;
            SVPane.Height = ActualHeight - Thickness * ActualHeight / 2;
        }

        void UpdateRGB(SolidColorBrush brush)
        {

        }

        void UpdateHSV(Tuple<double,double,double> hsv)
        {

        }

        void UpdateThickness(double thickness)
        {
            Shader.Thickness = PixelHelper.GetThickness( Wheel, thickness );
            SVPane.Width = this.ActualWidth - thickness;
            SVPane.Height = this.ActualHeight - thickness;
        }
        
    }
}
